package com.dormitory_15.dormitory.Controller.Student;

import com.dormitory_15.dormitory.Entity.Student;
import com.dormitory_15.dormitory.Service.Student.StudentService;
import com.dormitory_15.dormitory.Utils.CommonResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("/student")
public class StudentController {
    @Autowired
    private StudentService studentService;

    @PostMapping("/login")
    @ResponseBody
    public CommonResult login(@RequestBody Student student){
        try{
            String username = student.getUsername();//账号
            String password = student.getPassword();//密码
            if(username == null || password == null){
                return CommonResult.fail(400, "账号或密码不能为空");
            }
            Student userInfo = studentService.login(username, password);
            if(userInfo == null){
                return CommonResult.fail(400, "账号或密码错误");
            }
            return CommonResult.success(userInfo);
        }catch(RuntimeException e){
            e.printStackTrace();
            return CommonResult.fail(500, e.getMessage());
        }
    }
    @PostMapping("/register")
    @ResponseBody
    public CommonResult register(@RequestBody Student student){
        try{
            String id = UUID.randomUUID().toString().replace("-", "");
            student.setStudentId(id);//定义学生ID
            if(student.getUsername() == null || student.getPassword() == null){
                return CommonResult.fail(400, "账号或密码不能为空");
            }
            Integer register = studentService.register(student);
            if(register == 0){
                return CommonResult.fail(400, "注册失败");
            }
            return CommonResult.success(register);
        }catch (RuntimeException e){
            e.printStackTrace();
            return CommonResult.fail(500, e.getMessage());
        }
    }

    @GetMapping("/findStu")
    @ResponseBody
    public CommonResult getByNameOrOrigin(String name, String origin){
        try{
            if(name == null && origin == null){
                return CommonResult.fail(400, "参数不能为空");
            }
            List<Student> student = studentService.findByName(name, origin);
            if(student == null){
                return CommonResult.success("无该学生");
            }
            return CommonResult.success(student);
        }catch (RuntimeException e){
            e.printStackTrace();
            return CommonResult.fail(500, e.getMessage());
        }
    }

    @GetMapping("/list")
    @ResponseBody
    public CommonResult <PageInfo<Student>> getStudentByPage(@RequestParam(defaultValue = "1") int pageNum,
                                                             @RequestParam(defaultValue = "5") int pageSize){
        try{
            PageInfo<Student> studentsByPage = studentService.getStudentsByPage(pageNum, pageSize);
            return CommonResult.success(studentsByPage);
        }catch (RuntimeException e){
            e.printStackTrace();
            return CommonResult.fail(500, e.getMessage());
        }
    }
}
